﻿using Fur.Application.Dtos;
using Fur.Core;
using Fur.DatabaseAccessor;
using Fur.DynamicApiController;
using Mapster;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Fur.Application
{
    public class PersonAppService : IDynamicApiController
    {
        private readonly IRepository<Person> _personRepository;

        public PersonAppService(IRepository<Person> personRepository)
        {
            _personRepository = personRepository;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Person Insert(PersonInputDto input)
        {
            return _personRepository.InsertNow(input.Adapt<Person>()).Entity;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        public void Update([Required] int id, PersonInputDto input)
        {
            var entity = input.Adapt<Person>();
            entity.Id = id;

            _personRepository.Update(entity);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public List<Person> GetAll()
        {
            return _personRepository.AsEnumerable();
        }
    }
}
